package ge.telasi.tasks.ui.user;

import ge.telasi.tasks.TaskException;
import ge.telasi.tasks.ui.comp.CommonDialog;
import ge.telasi.tasks.ui.log.Loggable;
import ge.telasi.tasks.ui.log.LoggerUtils;
import java.awt.Component;
import javax.swing.JComponent;

/**
 * @author dimitri
 */
public class ServerItemDialog extends CommonDialog implements Loggable {

    public ServerItemDialog(Component parent) {
        super(parent, true);
        initComponents();
        setLogger(logger);
        getRootPane().setDefaultButton(btnSave);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        logger = new ge.telasi.tasks.ui.log.InlineLogger();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        txtName = new ge.telasi.tasks.ui.comp.SuggestionText();
        txtHost = new ge.telasi.tasks.ui.comp.SuggestionText();
        txtPort = new ge.telasi.tasks.ui.comp.SuggestionText();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("სერვერი თვისებები");

        jLabel1.setText("სერვერის სახელი:");

        jLabel2.setText("სერვერის მისამართი:");

        jLabel3.setText("პორტი:");

        btnSave.setText("შენახვა");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnCancel.setText("გაუქმება");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        txtName.setName("name"); // NOI18N
        txtName.setSuggestion("ჩაწერეთ სასურველი სახელი");

        txtHost.setName("host"); // NOI18N
        txtHost.setSuggestion("ჩაწერეთ სერვერის სახელი ან IP");

        txtPort.setName("port"); // NOI18N
        txtPort.setSuggestion("ჩაწერეთ პორტი, ძირითადად 3700");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(logger, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                            .addComponent(txtHost, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                            .addComponent(txtPort, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 271, Short.MAX_VALUE)
                        .addComponent(btnSave)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logger, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        approved = false;
        dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        LoggerUtils.clearLogger(getLogger(), allFields());
        try {
            validateForm();
            String name = txtName.getText();
            String host = txtHost.getText();
            int port = Integer.parseInt(txtPort.getText());
            serverItem = new ServerItem();
            serverItem.setName(name);
            serverItem.setHost(host);
            serverItem.setPort(port);
            approved = true;
            dispose();
        } catch (TaskException ex) {
            LoggerUtils.manageException(this, getLogger(), ex, allFields());
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private ge.telasi.tasks.ui.log.InlineLogger logger;
    private ge.telasi.tasks.ui.comp.SuggestionText txtHost;
    private ge.telasi.tasks.ui.comp.SuggestionText txtName;
    private ge.telasi.tasks.ui.comp.SuggestionText txtPort;
    // End of variables declaration//GEN-END:variables

    private boolean approved;
    private ServerItem serverItem;

    public boolean isApproved() {
        return approved;
    }

    public void setServerItem(ServerItem serverItem) {
        this.serverItem = serverItem;
        reset();
    }

    public ServerItem getServerItem() {
        return serverItem;
    }

    private void reset() {
        ServerItem display = serverItem == null ? new ServerItem() : serverItem;
        txtName.setText(display.getName());
        txtPort.setText(String.valueOf(display.getPort()));
        txtHost.setText(display.getHost());
    }

    private void validateForm() {
        String name = txtName.getText();
        String server = txtHost.getText();
        String portText = txtPort.getText();
        if (name == null || name.trim().length() == 0) {
            throw new TaskException("ჩაწერეთ სახელი", "name");
        }
        if (server == null || server.trim().length() == 0) {
            throw new TaskException("ჩაწერეთ სერვერის მისამართი", "host");
        }
        if (portText == null || portText.trim().length() == 0) {
            throw new TaskException("ჩაწერეთ პორტი", "port");
        }
        try {
            Integer.parseInt(portText);
        } catch (Exception fex) {
            throw new TaskException("პორტის მნიშვნელობად ჩაწერეთ ციფრი", "port");
        }
    }

    public JComponent[] allFields() {
        return new JComponent[] {txtName, txtHost, txtPort};
    }

}
